﻿Imports System.Data.OleDb

Partial Public Class Addwikiorblog
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub


    Protected Sub bt_Addcontent_Click(ByVal sender As Object, ByVal e As EventArgs) Handles bt_Addcontent.Click
        Dim conn = New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ToString())

        Dim ID_string As String = " SELECT [UserID] FROM aspnet_Users WHERE UserName = '" & User.Identity.Name & "' "
        Dim findIDCmd1 As New OleDbCommand(ID_string, Conn)
        conn.Open()
        Dim ID1 As OleDbDataReader = findIDCmd1.ExecuteReader
        ID1.Read()
        Dim UserID As Integer = ID1("UserID")
        Session("UserID") = UserID

        Dim mem As String
        Dim Details_string As String = " SELECT [MembershipID_FK] FROM Userdetails WHERE UserID_FK = " & UserID & " "
        Dim Details_command As OleDb.OleDbCommand = New OleDb.OleDbCommand(Details_string, conn)
        Dim Det_reader As OleDb.OleDbDataReader = Details_command.ExecuteReader()
        If Det_reader.HasRows Then
            While Det_reader.Read()
                mem = Det_reader.Item(0).ToString()
            End While


            'Dim sql As String = "INSERT INTO MasterTable (UserID_FK, TypeID_FK, Title, Description, Image) Values (@UserID_FK, @TypeID_FK, @Title, Description, @Image)"
            Dim sql As String = "INSERT INTO MasterTable (UserID_FK, TypeID_FK, Title, Description, [image], Reference, AddedDate, Count_page) Values (@UserID_FK, @TypeID_FK, @Title, @Description, [@image], @Reference, @AddedDate, @Count_page)"
            Dim fileLocationOnServer = Request.MapPath("Images/" & f_Picture.FileName)
            f_Picture.SaveAs(fileLocationOnServer)
            Dim relativePath As String = "Images/" & f_Picture.FileName
            Dim dbcmd = New OleDbCommand(sql, conn)

            dbcmd.Parameters.AddWithValue("@UserID_FK", UserID)
            dbcmd.Parameters.AddWithValue("@TypeID_FK", DropDownList1.SelectedValue)
            dbcmd.Parameters.AddWithValue("@Title", tb_Title.Text)
            dbcmd.Parameters.AddWithValue("@Description", Editor_description.Content.ToString)
            dbcmd.Parameters.AddWithValue("@Image", relativePath)
            dbcmd.Parameters.AddWithValue("@Reference", tb_References.Text)
            dbcmd.Parameters.AddWithValue("@AddedDate", Now.Date)
            dbcmd.Parameters.AddWithValue("@Count_page", 0)
            'conn.Open()


            If mem = "4" Then
                dbcmd.ExecuteNonQuery()
                Response.Redirect("List.aspx")
            ElseIf mem = "2" And (DropDownList1.SelectedValue = 1 Or DropDownList1.SelectedValue = 2 Or DropDownList1.SelectedValue = 3) Then
                dbcmd.ExecuteNonQuery()
                Response.Redirect("List.aspx")
            ElseIf mem = "1" And (DropDownList1.SelectedValue = 2 Or DropDownList1.SelectedValue = 3) Then
                Label1.Visible = True

            Else
                Label1.Visible = True

            End If
        End If

    End Sub
End Class